//
//  LocalFileViewController.m
//  LYiChat
//
//  Created by 李政 on 16/4/28.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "LocalFileViewController.h"

#import "TableViewCell.h"
#import "DownloadManager.h"

#import "Util.h"

@interface LocalFileViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation LocalFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [_myTable registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButton)];
    
    self.title = @"本地文件";
}


- (void)leftButton
{
    self.block();
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [DownloadManager shared].count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [DownloadManager shared][indexPath.row];
    
    
    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:dic[@"image_url"]]];
    
    cell.nameLabel.text = dic[@"tname"];
    cell.descLabel.text = dic[@"description"];
    cell.sizeLabel.text = [NSString stringWithFormat:@"%.2fMB",(float)[dic[@"length"] longLongValue]/1024];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = [DownloadManager shared][indexPath.row];
    
    [Util deleteFileWithDic:dic];
    
    
    [[DownloadManager shared] removeObjectAtIndex:indexPath.row];
    
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
